package com.wangbo.proxy;

/**
 * 代理模式-动态代理
 */
public class Client {
    public static void main(String[] args) {
        /**
         * ------------CGLib Proxy------------
         * 添加用户
         * ~~~模拟增强~~~
         * 添加成功，uid:1
         * 删除用户
         * 删除结果：true
         * -----------------JDK Proxy------------------
         * 添加用户
         * ~~~模拟增强~~~
         * 添加成功，uid:2
         * 删除用户
         * 删除结果：true
         */
        testCGLibProxy();
        testJDKProxy();
    }

    private static void testCGLibProxy() {
        UserService userService = new CGLibProxy<UserService>().createProxy(UserServiceImpl.class);
        System.out.println("------------CGLib Proxy------------");
        long uid = userService.addUser(1L, "Jack");
        System.out.printf("添加成功，uid:%d%n",uid);
        boolean removeSuccess = userService.removeUser(1L);
        System.out.printf("删除结果：%s%n", removeSuccess);
    }

    private static void testJDKProxy() {
        UserService userService = new JDKProxy<UserService>().createProxy(new UserServiceImpl());
        System.out.println("-----------------JDK Proxy------------------");
        long uid = userService.addUser(2L, "Tom");
        System.out.printf("添加成功，uid:%d%n",uid);
        boolean removeSuccess = userService.removeUser(2L);
        System.out.printf("删除结果：%s%n", removeSuccess);
    }
}
